package com.tjsinfo.tjsoa.oldOADocument.shouwen.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.oldOADocument.fawen.vo.FuJianFile;
import com.tjsinfo.tjsoa.oldOADocument.shouwen.vo.ShouWenInfo;

public class TChaYueWenJianBO implements ITChaYueWenJianBO	{

	private ISpringDao springDao;
	

	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}

	/**
	 * 文件查询
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wenyear 年度
	 * @return
	 */
	public List<ShouWenInfo> selectChaYueWenJian(Integer first,Integer max,String laiwenbiaoti,String shouwenbianhao,String laiwenzihao,String laiwenjiguan,String maintypeid){
		
		String sql =  new String("EXEC dbo.swchayuewenjian @first = ?,@limit = ?,@biaotiselect = ?,@shouwenbianhao = ?,@laiwenzihao = ?,@laiwenjiguan=? ,@sortid=?");
		List<ShouWenInfo> shouwenList = springDao.querySQL(sql, new Object[]{first,max,laiwenbiaoti, shouwenbianhao, laiwenzihao,laiwenjiguan,maintypeid}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				    ShouWenInfo shouWenInfo = new ShouWenInfo();
				    shouWenInfo.setShouwenbianhao(arg0.getString("shouwenbianhao"));
				    shouWenInfo.setLaiwenbiaoti(arg0.getString("biaoti"));
				    shouWenInfo.setLaiwenzihao(arg0.getString("laiwenzihao"));
				    shouWenInfo.setLaiwenjiguan(arg0.getString("laiwenjiguan"));
				    shouWenInfo.setLaiwenriqi(ToolKit.FormatDate(arg0.getDate("laiwenriqi"),"yyyy-MM-dd"));
				    shouWenInfo.setId(arg0.getString("id"));
					return shouWenInfo;
				
			}
			
		});
		
		return shouwenList;
	}
	
	/**
	 * 文件查询结果总数
	 * @param biaoti 标题
	 * @param wenyear 年度
	 * @return
	 */
	public Integer selectChaYueWenJianCount(String laiwenbiaoti,String shouwenbianhao,String laiwenzihao,String laiwenjiguan,String maintypeid){
		String sql =  new String("EXEC dbo.swchayuewenjiancount @biaotiselect = ?,@shouwenbianhao = ?,@laiwenzihao = ?,@laiwenjiguan=? ,@sortid=?");
		List<Integer> countList = springDao.querySQL(sql, new Object[]{laiwenbiaoti, shouwenbianhao, laiwenzihao,laiwenjiguan,maintypeid}, new RowMapper(){
		
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("count");
				
			}
			
		});
		
		return countList.get(0);
	}

	/**
	 * 根据id查询文件
	 */
	public ShouWenInfo selectOneById(String id) {
		// TODO Auto-generated method stub
		ShouWenInfo shouwenreturn = new ShouWenInfo();
		String sql =  new String("select id,subject,reserve_1 from dbo.t_document_fasong_main where id =?");
		List<ShouWenInfo> shouwenList = springDao.querySQL(sql, new Object[]{id}, new RowMapper(){
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
					ShouWenInfo shouWenInfo = new ShouWenInfo();
					shouWenInfo.setId(arg0.getString("id"));
					shouWenInfo.setLaiwenbiaoti(arg0.getString("subject"));
					shouWenInfo.setParentid(arg0.getString("reserve_1")); //附件关联id
					return shouWenInfo;
				}
			});
		
		if (shouwenList.size()>0){
			shouwenreturn = shouwenList.get(0);
			String parentid=shouwenreturn.getParentid();
			String sqlfujian =  new String("select id, alias,[file_name] as filename, url_Path from dbo.t_com_file where parent_id =?");
			List<FuJianFile> fujianlist = springDao.querySQL(sqlfujian, new Object[]{parentid}, new RowMapper(){
				public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
					FuJianFile fujian= new FuJianFile();
					fujian.setAlias(arg0.getString("alias"));
					fujian.setFileName(arg0.getString("filename"));
					fujian.setUrlPath(arg0.getString("url_Path"));
					return fujian;
				}
			});
			Set fawenFujians = new HashSet(0);
			for(FuJianFile fujianfile:fujianlist){
				fawenFujians.add(fujianfile);
			}
			shouwenreturn.setFawenFujians(fawenFujians);
		}
		
		return shouwenreturn;
	}
	
	
}
